<?php
ob_start();
session_start();

if (!isset($_SESSION["userid"])){
	echo "No session register";
   header("location:index.php");	
}
else{
	$name = $_SESSION["name"];
	$imgurl =$_SESSION["imageurl"];
	
}
$userid = $_SESSION["userid"];
?>

<html>

<head>
<title>Homepage</title>
<LINK href="fbStyle.css" rel=stylesheet> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<?php include 'class_lib.php';?>

<script  type="text/javascript" language="javascript" >

function formToSubmit(){

   if(document.inserForm.newpost.value==""){
	alert("Please insert some text !");
 
   else{
	return document.insertForm;
	 
   }	   

}
function linkToUser(){
	
}
</script>


</head>

<body>

<div id="header">
<?php include 'header.php';?>
</div>
<?php 


$useridperson = $_GET["useridperson"];
if ($useridperson!= NULL) {
	$_SESSION["useridperson"] = $useridperson;
}
else{
    $useridperson =  $_SESSION["useridperson"] ;
}

if (isset($useridperson)) {
	$connectUser = new Connection();
    $queryUser = "select * from user where userid='$useridperson'";
    $resultUser = $connectUser->requestdata($queryUser);

    $index = 0;
    $userIDperson = mysql_result($resultUser,$index,"userid");
    $nameperson = mysql_result($resultUser, $index,"name");
    $profileUrlperson = mysql_result($resultUser, $index,"imageurl");
    $birthdayperson = mysql_result($resultUser, $index,"birthday");
    $genderperson = mysql_result($resultUser, $index,"gender");
    $nationalperson = mysql_result($resultUser, $index,"national");
    $emailperson = mysql_result($resultUser, $index,"email");
    $workingperson = mysql_result($resultUser, $index,"working");
    $relationshipperson = mysql_result($resultUser, $index,"relationship");


}

?>
<div id="left">
      <div class="leftuserinfoframe">
            <img class="profileImage" src='<?php echo $profileUrlperson ?>'></img>
             <?php 
             echo "<a class='profileName' href='personPage.php?useridperson=$userIDperson'>$nameperson
             
             </a>";
             ?>
            
             <div class="clear"></div>
             <div class="userinfodetailframe">
             
             <p>Giới tính :  <?php echo $genderperson?> </p>
             <p>Ngày sinh :  <?php echo $birthdayperson?></p>
             <p>Quốc tịch :  <?php echo $nationalperson?></p>
             
             
             <p>Email:  <?php echo $emailperson?></p>
             <p>Nơi làm việc :  <?php echo $workingperson?></p>
            
             
             <p>Tình trạng quan hệ :  <?php echo $relationshipperson?></p>
             
             
             </div>
      </div>
</div>

<div id="center">

<div class="insertframe">
   <form name="insertForm" action="personPage.php" method="Post">
   <h5 >Post lên tường</h5>
   <textarea name="newpost" class="insertInput" rows= '4' ></textarea>
   ImageURL  <input type="text" name="urlimagelink"></input>
   <a href="#" onclick="document.insertForm.submit(); return yes;"  class="button">Post</a>
  </form>
</div>




<?php 



$connect = new Connection();


if (isset($HTTP_POST_VARS["newpost"])) {
$newpost = $HTTP_POST_VARS["newpost"];
if ($newpost != "") {
	$userid = $_SESSION["userid"];
	
	
	$imageurlpost = $HTTP_POST_VARS["urlimagelink"];
	date_default_timezone_set("Asia/Ho_Chi_Minh");
	$posttime = date('Y-m-d H:i:s');
	$postid = $posttime.$userid;
	
	
    if ($userid != $useridperson) {
    	$newpostquery = "INSERT INTO `fbdemodatabase`.`posts` (`post_id`, `userpostid`, `content`, `imageposturl`, `likesnumber`, `ispublic`, `posttime`,`friendpostid`) VALUES ('".$postid."', '".$userid."','".$newpost."', '".$imageurlpost."', '0', '1', '$posttime','$useridperson');";
    	$postresult = $connect->requestdata($newpostquery);
    }
	else{
        $newpostquery = "INSERT INTO `fbdemodatabase`.`posts` (`post_id`, `userpostid`, `content`, `imageposturl`, `likesnumber`, `ispublic`, `posttime`) VALUES ('".$postid."', '".$userid."','".$newpost."', '".$imageurlpost."', '0', '1', '$posttime');";
        $postresult = $connect->requestdata($newpostquery);
    }
	unset($GLOBALS['newpost']);
	header("location:personPage.php");
	}
}


$query = "Select * from user,posts where user.userid=".$useridperson." and (user.userid = posts.userpostid or user.userid=posts.friendpostid) order by posttime desc";
$result = $connect->requestdata($query);
if ($result) {
	   $number = mysql_num_rows($result);
       $index = 0;
       while ($index < $number) {
       
        $userpostid = mysql_result($result, $index,"userpostid");
       
        $usergetpost = new User($userIDperson, $nameperson, $profileUrlperson);
       
       	$content = mysql_result($result, $index,"content");
       	$imagepost = mysql_result($result, $index,"imageposturl");
       	$posttime = mysql_result($result, $index,"posttime");
       	$friendid = mysql_result($result, $index,"friendpostid");
       	
       	if ($friendid) {
            if ($friendid == $useridperson){
                 // usergetpost la nguoi nhan
                 $queryfriend = "select * from user where userid=$userpostid";
                 $resultfriend = $connect->requestdata($queryfriend);
                 $firth = 0;
                 $friendpostid = mysql_result($resultfriend, $firth,"userid");
                 $friendpostname = mysql_result($resultfriend, $firth,"name");
                 $friendprofileurl = mysql_result($resultfriend, $firth,"imageurl");

                 $friendpost = new User($friendpostid, $friendpostname, $friendprofileurl);
                 
                 $thepost = new Post($userpostid, $friendpost, $content, $imagepost, $posttime,$usergetpost);
                  
                 $thepost->showPost();
                

            }
            else{
                // usergetpost la nguoi post
                $queryfriend = "select * from user where userid=$friendid";
                $resultfriend = $connect->requestdata($queryfriend);
                $firth = 0;
                $friendpostid = mysql_result($resultfriend, $firth,"userid");
                $friendpostname = mysql_result($resultfriend, $firth,"name");
                $friendprofileurl = mysql_result($resultfriend, $firth,"imageurl");

                $friendpost = new User($friendpostid, $friendpostname, $friendprofileurl);

                $thepost = new Post($userpostid, $usergetpost, $content, $imagepost, $posttime,$friendpost);
                 
                $thepost->showPost();
            }

             
       	}
       	else{
 
            $thepost = new Post($userpostid, $usergetpost, $content, $imagepost, $posttime,'');
 
            $thepost->showPost();
        }

       	$index++;
       }

}
else{
  echo "Câu lênh:".$query;
}


?>



</div>

</body>
</html>